package org.hxzon.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FilenameUtils;

public class FileNameUtil {

    public static String windowsFileName(String name) {
        //\ * / ? | : " < >
        return name.replaceAll("[\\\\\\*\\/\\?\\|\\:\\\"\\<\\>]", "-");
    }

    public static String replaceFileExtension(String filename, String newExtension) {
        return FilenameUtils.removeExtension(filename) + "." + newExtension;
    }

    public static String generateFileName(String fileName) {
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String formatDate = format.format(new Date());

        int position = fileName.lastIndexOf(".");
        String extension = fileName.substring(position);
        return fileName + formatDate + extension;
    }

    public static boolean checkFileExtension(String filename, String extensions[]) {
        String extension = FilenameUtils.getExtension(filename);
        for (String accept : extensions) {
            if (accept.equalsIgnoreCase(extension)) {
                return true;
            }
        }
        return false;
    }
}
